package com.datang.petstore.controller;


import com.datang.petstore.aop.ForbidAnnotation;
import com.datang.petstore.entity.UserInfo;
import com.datang.petstore.model.GetUserInfoRequest;
import com.datang.petstore.model.LoginUserRequest;
import com.datang.petstore.resultpage.Result;
import com.datang.petstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-10-02 14:42
 * @notify 用户相关api
 * @version 1.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /*
     * @author 顶风少年
     * @date 2019/10/8 13:58
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param open_source 三方标识
     * @param openId   三方唯一id
     * @result com.datang.petstore.utils.Result 返回一个用户信息
     * @notify 登录接口,接收openid和opensource.查询,如果查到则返回,查不到则注册.
     * version 1.0
     */
    @PostMapping(value="login")
    public Result Login(@RequestBody LoginUserRequest loginUserRequest) {
        UserInfo userInfo = userService.loginUserInfo(loginUserRequest.getOpenId(), loginUserRequest.getOpenSource());
        return Result.ok(userInfo);
    }

    /*
     * @author 顶风少年
     * @date 2019/10/22 15:57
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result com.datang.petstore.utils.Result
     * @notify 根据用户id获取用户信息
     * version 1.0
     */
    @GetMapping(value="user/{userId}")
    @ForbidAnnotation
    public Result getUser(GetUserInfoRequest getUserInfoRequest) {
        UserInfo userInfo = userService.getUserInfo(getUserInfoRequest.getUserId());
        return Result.ok(userInfo);
    }


}
